$(function(){
    $('#listGrid').datagrid({
        toolbar: '#toolbar',
        title: '账套列表',
        iconCls: 'icon-ok',
        rownumbers: true,
        fit:true,
        singleSelect: true,
        url: '/sys/client/list',
        queryParams:{
            _token : $('meta[name="csrf-token"]').attr('content')
        },
        columns: [[
            { title: 'id', field: 'id', hidden: true },
            { title: '账套名称', field: 'name',width: 130 },
            { title: '公司名称', field: 'company_name', width: 130 },
            { title: '公司Logo', field: 'company_logo', width: 130 },
            { title: '开票地址', field: 'company_address', width: 130 },
            { title: '开票电话', field: 'company_phone', width: 130 },
            { title: '开户银行', field: 'company_bank', width: 130 },
            { title: '开户帐号', field: 'bank_number', width: 130 },
            { title: '用户前缀', field: 'user_prefix', width: 130 },
            { title: '有效', field: 'is_active', width: 50, formatter: format_active }
        ]],
        onLoadSuccess: function (data) {
            if (data === null || data.total == 0) {
                var body = $(this).data().datagrid.dc.body2;
                body.find('table tbody').append('<tr><td width="' + body.width() + '" style="height: 35px; text-align: center;"><h1>暂无数据</h1></td></tr>');
            }
        }
    });
})

function openAdd(){
    $('#form_dialog').dialog({
        title: '添加信息',
        width: 450,
        height: 300,
        closed: false,
        cache: false,
        iconCls:'icon-application-form-add',
        href: '/sys/client/add',
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function openEdit(){
    var rows = $('#listGrid').datagrid('getSelected');
    if (null == rows) {
        $.messager.alert("提示信息", "请选择需要编辑的行" , "info");
        return;
    }
    $('#form_dialog').dialog({
        title: '编辑信息',
        width: 450,
        height: 300,
        closed: false,
        cache: false,
        href: '/sys/client/edit?client_id='+rows.id,
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: save
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $('#form_dialog').dialog('close');
            }
        }]
    });
}

function save(){
    $.ajax({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        url:'/sys/client/save',
        type:'post',
        data:$('#info-form').serialize(),
        dataType:'json',
        success:function (data) {
            if(data.code == 200){
                $('#listGrid').datagrid("load",{_token : $('meta[name="csrf-token"]').attr('content')})
                $('#form_dialog').dialog('close');
            }else{
                $.messager.alert("提示信息", data.data , "error");
            }
        }
    });
}

/**
 * Name 删除记录
 */
function remove(){
    var items = $('#listGrid').datagrid('getSelected');
    if (items.length === 0) {
        $.messager.alert('警告','请选择一行');
    }else {
        $.messager.confirm('信息提示','确定要删除该记录？', function(result){
            if(result){

                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    type:'post',
                    url:'/sys/client/remove',
                    data:{
                        id:items.id
                    },
                    success:function(data){
                        if(data.code === 200){
                            $.messager.alert('信息提示','删除成功！');
                            $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')});
                        }
                        else {
                            $.messager.alert('信息提示','删除失败,请稍后再试！');
                            $('#listGrid').datagrid('reload',{_token : $('meta[name="csrf-token"]').attr('content')})
                        }
                    }
                });
            }
        });
    }
}